package com.zjdiepu.www.biz.system;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.system.SysAppMenuMapper;
import com.zjdiepu.www.ibiz.system.ISysAppMenuService;
import com.zjdiepu.www.model.system.dto.SysAppMenu;
import com.zjdiepu.www.model.system.ext.SysAppMenuExt;

/**
 * sys_app_菜单Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年10月30日 17时22分<br/>
 * @since 1.0<br/>
 */
@Service
public class SysAppMenuServiceImpl extends BaseService implements ISysAppMenuService {
	public static final Logger logger = LoggerFactory.getLogger(SysAppMenuServiceImpl.class);

	@Autowired
	private SysAppMenuMapper sysAppMenuMapper;
	
	@Override
	public List<SysAppMenu> selectByRoleId(String roleId) {
		return sysAppMenuMapper.selectByRoleId(roleId);
	}
	
	@Override
	public void saveUpdateAndDelete(SysAppMenu sysAppMenu, OperateType operType) {
		switch (operType) {
		case add:
			sysAppMenu.initInsertData(sysAppMenu.getModifier(), sysAppMenu.getModifierName());
			sysAppMenuMapper.insert(sysAppMenu);
			break;
		case edit:
			sysAppMenu.initUpdateData(sysAppMenu.getModifier(), sysAppMenu.getModifierName());
			// 影响的记录数
			int updateCount = sysAppMenuMapper.updateByPrimaryKeySelective(sysAppMenu);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			sysAppMenu.initDeleteData(sysAppMenu.getModifier(), sysAppMenu.getModifierName());
			// 影响的记录数
			int deleteCount = sysAppMenuMapper.updateByPrimaryKeySelective(sysAppMenu);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<SysAppMenu> selectByExt(SysAppMenuExt sysAppMenuExt) {
		return sysAppMenuMapper.selectByExt(sysAppMenuExt);
	}
	
	@Override
	public int countByExt(SysAppMenuExt sysAppMenuExt){
		return sysAppMenuMapper.countByExt(sysAppMenuExt);
	}
	
	@Override
	public SysAppMenu selectById(String id) {
		return sysAppMenuMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			sysAppMenuMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(SysAppMenu sysAppMenu) {
		return sysAppMenuMapper.countRefined(sysAppMenu);
	}

	@Override
	public SysAppMenu selectRefined(SysAppMenu sysAppMenu) {
		return sysAppMenuMapper.selectRefined(sysAppMenu);
	}
	
	@Override
	public int deletedRefined(SysAppMenu sysAppMenu) {
		return sysAppMenuMapper.deletedRefined(sysAppMenu);
	}

	@Override
	public boolean repeat(SysAppMenu sysAppMenu) {
		int count = countRefined(sysAppMenu);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(sysAppMenu.getId())) {
			return true;
		}
		SysAppMenu dbItem = selectRefined(sysAppMenu);
		if(null == dbItem) {
			return false;
		}
		return !sysAppMenu.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return sysAppMenuMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<SysAppMenu> sysAppMenus) {
		return sysAppMenuMapper.insertBatch(sysAppMenus);
	}
}
